﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;

namespace ShowroomMobile
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        private int UserID
        {
            get
            {
                return string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UserID > 0)
                {
                    Users user = new Users();
                    user.LoadByPrimaryKey(UserID);
                    if (user.RowCount > 0)
                    {
                        txtUsername.Text = user.s_UserName;
                        txtFullName.Text = user.s_FullName;
                        txtPassword.Text = user.s_Password;
                        txtConfirmPassword.Text = user.s_Password;
                        txtPassword.Enabled = false;
                        txtConfirmPassword.Enabled = false;
                        rfvPassword.ControlToValidate = "txtUsername";
                        rfvConfirmPassword.ControlToValidate = "txtUsername";
                        btnResetPassword.Visible = true;
                    }
                    else
                    {
                        Response.Redirect("~/404.aspx");
                    }
                }
            }
        }

        protected void btnResetPassword_OnClick(object sender, EventArgs e)
        {
            txtPassword.Enabled = true;
            txtConfirmPassword.Enabled = true;
            rfvPassword.ControlToValidate = "txtPassword";
            rfvConfirmPassword.ControlToValidate = "txtConfirmPassword";
            btnResetPassword.Visible = false;
            divPwd.Visible = true;
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                Users user = new Users();
                if (UserID > 0)
                {
                    user.LoadByPrimaryKey(UserID);
                }
                else
                {
                    user.AddNew();
                    user.MasterAdmin = false;
                }
                if (txtPassword.Enabled)
                {
                    user.s_Password = Utilities.CreateMD5Hash(txtPassword.Text.Trim());
                }
                user.s_UserName = txtUsername.Text.Trim();
                user.s_FullName = txtFullName.Text.Trim();
                user.Deleted = false;
               
                user.Save();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Update successfully!');", 1), true); 
                if (UserID == 0)
                    txtUsername.Text = txtFullName.Text = string.Empty;

                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), "window.parent.closeIframe();", true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("UpdateSlideShow.aspx.cs", "btnSave_OnClick", ex.StackTrace);
            }
        }
    }
}